package document;

import java.util.ArrayList;
import java.util.List;

import parser.Parser;
import main_app.MainApp;

/**
 * Class represents a document (article) as a histogram of words and a distribution over topics
 */
public class Document {
	
	private ArrayList<String> realTopics; // list of topics originally assigned by corpus
	private int[] wordsHistogram; // frequency of each word
	private double[] topicsDistribution; // W_ti
	private long index; // index in data set
	private long length; // number of words in document
	
	/**
	 * Constructor- creates a new Document object
	 * @param realTopics list of topics originally assigned by corpus
	 * @param wordsHistogram frequency of each word
	 * @param index index in data set
	 */
	public Document(ArrayList<String> realTopics, int[] wordsHistogram,
			long index) {
		
		// set members as given values
		this.realTopics = realTopics;
		this.wordsHistogram = wordsHistogram;
		this.index = index;
		
		// initialize topics distribution
		topicsDistribution = new double[MainApp.NUM_TOPICS];
				
		// calculate document length
		for (Integer wordFreq : wordsHistogram) {
			length += wordFreq;
		}
	}
	
	/**
	 * Returns frequency of word matching given index
	 * @param wordIndex index of word to check
	 * @return frequency of indicated word, or 0 if index is invalid
	 */
	public int getFreq(int wordIndex) {
		
		// check boundaries
		if (wordIndex < 0 || wordIndex >= Parser.VOCAB_SIZE) {
			return 0;
		}
		
		// return frequency of word matching given index
		return wordsHistogram[wordIndex];
	}
	
	/**
	 * Returns probability of topic matching given index
	 * @param topicIndex index of topic to check
	 * @return probability of indicated topic, or 0 if index is invalid
	 */
	public double getTopicProb(int topicIndex) {
		
		// check boundaries
		if (topicIndex < 0 || topicIndex >= MainApp.NUM_TOPICS) {
			return 0;
		}
		
		// return probability of topic matching given index
		return topicsDistribution[topicIndex];
	}
	
	/**
	 * Returns number of words in document
	 * @return number of words in document
	 */
	public long getLength() {
		return length;
	}
	
	/**
	 * Returns document index in data set
	 * @return document index in data set
	 */
	public long getIndex() {
		return index;
	}
	
	/**
	 * Returns topics originally assigned by corpus
	 * @return list of topics originally assigned by corpus
	 */
	public List<String> getRealTopics() {
		return realTopics;
	}
}
